<template>
  <div class="role-management-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete($event)"
        >
          批量删除
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="分组名称"
        prop="group_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="排序"
        prop="sort"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="状态" prop="status">
        <template #default="{ row }">
          <el-switch
            v-model="row.status"
            active-text="显示"
            :active-value="1"
            inactive-text="隐藏"
            :inactive-value="0"
            @change="switchChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { deleteGroup, getList, switchGroup } from '@/api/group'
  import Edit from './components/groupEdit'

  export default {
    name: 'Group',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        selectRows: '',
        switchData: {
          id: '',
          status_name: 'status',
          status_value: '',
        },
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          page: 1,
          perpage: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.perpage = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteGroup({ ids: row.id })
            if (data.is_success) {
              this.$baseMessage(
                '删除成功',
                'success',
                'vab-hey-message-success'
              )
              await this.fetchData()
            }
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { data } = await deleteGroup({ ids })
              if (data.is_success) {
                this.$baseMessage(
                  '删除成功',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              }
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList({})
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
      async switchChange(row) {
        this.switchData.id = row.id
        this.switchData.status_value = row.status
        await switchGroup(this.switchData)
        const { data } = await getList({})
        this.list = data.items
        this.total = data.total
      },
    },
  }
</script>
